function initRaid()
{   
	System.Gadget.settingsUI = "Settings.html";
	System.Gadget.onSettingsClosed = settingsClosed;

	debug( "test1" );
	
	var myTimerFunction = function() { updateRaidInfo(); };
	myTimerFunction.periodical( 60000*10 ); // 10 minutes	
	myTimerFunction();
}

function settingsClosed(event)
{
    if( event.closeAction == event.Action.commit )
    {  
        updateRaidInfo( );
    }
}


function updateRaidInfo( )
{
	var targeturl = "http://site.aspire-guild.se/signup/raidrss.php";	
		
	var myRequest = new Request({
		"method": 'get', 
		"url": targeturl,
		"onSuccess": function( responseText, responseXML ) {
			debug( "test2" );
			var raiddata = parseRssXml( responseXML );
			loadInterface( raiddata );
		}
	});
	myRequest.send('');
}

function debug( message )
{
	var elm = $('gadgetContent');
	
	if ( elm != null )
		elm.set( "html", message );
}

/**
 * Create Javascript Object containing the following info
 *
 * raid.dag
 * raid.datum
 * raid.tijd
 * raid.raidtype
 * raid.raidname
 * raid.link
 * raid.status = raiding|reserve|signedin|unknown|signedout
 */
function parseRssXml( xml ) 
{
	if ( xml == null )	{
		alert( "no xml data" );
		return; 
	}	
	
	var charactername = System.Gadget.Settings.read( "charactername" );
	
	var raids = new Array();
	
	// get all the raids from this item
	var items = xml.getElementsByTagName( "item" );
	
	for( i = 0; i < items.length && i < 5; i++ )
	{
		debug( "test" + (i+3) );
	
		var raid = new Hash();
	
		// get the raid information
		var item = items.item(i);
	
		// retrieve title content, apparently you need all this to get: <title>text</title>
		var title = item.getElementsByTagName( "title" ).item(0).childNodes.item(0).nodeValue;
		raid.link = new String( item.getElementsByTagName( "link" ).item(0).childNodes.item(0).nodeValue ).trim().replace( "www.aspire", "site.aspire" );
		var description = item.getElementsByTagName( "description" ).item(0).childNodes.item(0).nodeValue;
		
		// parse title
		// "Tue 19/01/10 19:00 - 10 Man - Icecrown Citadel"
		var titleParts = title.split( " " );
		raid.dag = titleParts[0];
		raid.datum = titleParts[1];
		raid.tijd = titleParts[2];
		raid.raidtype = "";
		raid.raidname = "";
		
		// parse the "10 Man" part
		for ( index = 4; index < titleParts.length; index++ )
		{
			if ( titleParts[index] == "-" )	break;				
			raid.raidtype += titleParts[index] + " ";
		}
		
		index++; // skip - 
		
		// parse the "Icecrown Citadel" part
		for ( ; index < titleParts.length; index++ )
			raid.raidname += titleParts[index] + " ";
				
		// parse description
		raid.status = "unknown";
		var descrParts = description.split( "|" );
		for ( j = 1; j < descrParts.length; j++ )
		{
			if ( charactername && charactername.length > 0 && descrParts[j].indexOf( charactername ) >= 0 )
			{
				if ( descrParts[j].indexOf( "Raiding:" ) >= 0 )
					raid.status = "raiding";
				else if ( descrParts[j].indexOf( "Reserve:" ) >= 0 )
					raid.status = "reserve";
				else if ( descrParts[j].indexOf( "Signed IN:" ) >= 0 )
					raid.status = "signedin";
				else if ( descrParts[j].indexOf( "Status Unknown:" ) >= 0 )
					raid.status = "unknown";
				else if ( descrParts[j].indexOf( "Signed OUT:" ) >= 0 )
					raid.status = "signedout";
					
				break;
			}
		}
		
		// add this raid to the whole
		raids.include( raid );
	}
	
	return raids;
}


function loadInterface( raids )
{
	var body = new Element( document.getElementById( "raidingschedule" ) );
	body.empty();

	raids.each( function( raid ) {
	
		if ( raid.status == "raiding" || raid.status == "signedin" )
			raid.image = "tick-new.png";
		else if ( raid.status == "reserve" )
			raid.image = "reserve.png";
		else
			raid.image = "cross-new.png";
		
		var elm = body.appendChild( new Element( 'div', {
			"class": "raid"			
		}));
	
		elm.appendChild( new Element( 'div', {
			"class": "afbeelding"
		}))
		.appendChild( new Element( 'img', {
			"src": raid.image,
			"align": "absmiddle"
		}));
	
		elm.appendChild( new Element( 'div', {
			"class": "dag",
			"html": raid.dag
		}));
	
		elm.appendChild( new Element( 'div', {
			"class": "tijd",
			"html": raid.tijd
		}));
		
		elm.appendChild( new Element( 'div', {
			"class": "grootte",
			"html": raid.raidtype
		}));
		
		elm.appendChild( new Element( 'div', { "class": "clr" } ) );
		
		elm.appendChild( new Element( 'div', {
			"class": "raidnaam"
		}))
		.appendChild( new Element( 'a', {
			"href": raid.link,
			"class": "raidlink",
			"html": raid.raidname
		}));
		
		elm.appendChild( new Element( 'div', { "class": "clr" } ) );
	});
		
}
